package com.csti.delio.main.client.util;

import com.extjs.gxt.ui.client.binding.Converter;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.Field;

/**
 * 
 * @author akartal
 */
public class ComboboxFieldBinding extends FieldBinding {

	@SuppressWarnings("unchecked")
	public ComboboxFieldBinding(Field field, String property) {
		super(field, property);
		if (field instanceof ComboBox) {
			final ComboBox<ModelData> combo = (ComboBox<ModelData>) field;
			setConverter(new Converter() {

				@Override
				public Object convertFieldValue(Object value) {
					if (value instanceof ModelData) {
						ModelData val = (ModelData) value;
						return val.get(combo.getValueField());
					} else {
						return value;
					}
				}

				@Override
				public Object convertModelValue(Object value) {
					return combo.getStore().findModel(combo.getValueField(),
							value);
				}

			});
		}
	}

}